package com.example.tangjiangxu.asynctaskdemo;

import android.app.Activity;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";
    private Button bnMain;
    private Button bnSub;
    private Button bnExecute;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.e(TAG, "onCreate: 主线程id:" + Thread.currentThread().getName());
        bnMain = (Button) findViewById(R.id.bn_main);
        bnSub = (Button) findViewById(R.id.bn_sub);
        bnExecute = (Button) findViewById(R.id.bn_execute);
        bnMain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < 127; i++) {
                    new MyAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, i * 100);
                }

            }
        });
        bnSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Log.e(TAG, "run线程id:" + Thread.currentThread().getName());
                        new MyAsyncTask().execute(-100);
                    }
                }).start();
            }
        });
        bnExecute.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int CPU_COUNT = Runtime.getRuntime().availableProcessors();
                int CORE_POOL_SIZE = CPU_COUNT + 1;
                final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;
                final int KEEP_ALIVE = 1;

                Executor exec = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, 1, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(128),new ThreadFactory() {
                    private final AtomicInteger mCount = new AtomicInteger(1);

                    public Thread newThread(Runnable r) {
                        Log.e(TAG, "newThread: ");
                        return new Thread(r, "AsyncTask #");
                    }
                });

                for (int i = 0; i < 500; i++) {
                    new MyAsyncTask().executeOnExecutor(exec, i);
                }

            }
        });

    }

    /**
     * 自定义的任务
     */
    class MyAsyncTask extends AsyncTask<Object, Integer, Void> {

        @Override
        protected void onPreExecute() {
            Log.e(TAG, "onPreExecute线程id:" + Thread.currentThread().getName());
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            Log.e(TAG, "onPostExecute线程id:" + Thread.currentThread().getName());
            super.onPostExecute(aVoid);
        }

        /**
         * 肯定是自现场
         * @param values
         */
        @Override
        protected void onProgressUpdate(Integer... values) {
            Log.e(TAG, "onProgressUpdate线程id:" + Thread.currentThread().getName());
            super.onProgressUpdate(values);
        }

        @Override
        protected Void doInBackground(Object... params) {
            Log.e(TAG, "doInBackground线程id1:" + Thread.currentThread().getName());
            for(int i = Integer.parseInt(params[0]+""); i < Integer.parseInt(params[0]+"") + 1; i++) {
                try {
                    Log.e(TAG, "doInBackground线程id2:" + Thread.currentThread().getName());
                    Thread.sleep(60000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Log.e(TAG, "doInBackground: i:" + i );
                publishProgress(i);
            }
            return null;
        }
    }
}
